package com.mtons.mblog.web.controller.learn;

import com.mtons.mblog.base.lang.Result;
import com.mtons.mblog.modules.entity.LearnContentType;
import com.mtons.mblog.modules.service.LearnContentTypeService;
import com.mtons.mblog.web.controller.BaseController;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller("learnContentTypeController")
@RequestMapping("/learn/contenttype")
public class LearnContentTypeController extends BaseController {

    @Resource
    private LearnContentTypeService learnContentTypeService;

    @GetMapping(value={"/","/list"})
    public String paging(String name, ModelMap model) {
        Pageable pageable = wrapPageable();
        Page<LearnContentType> page = learnContentTypeService.paging(pageable, name);
        model.put("name", name);
        model.put("page", page);
        return "/learn/contenttype/list";
    }

    @RequestMapping("/view")
    public String view(String id, ModelMap model) {
        if (id != null) {
            LearnContentType view = learnContentTypeService.getOne(id);
            model.put("view", view);
        }
        return "/learn/contenttype/view";
    }

    @RequestMapping("/update")
    public String update(LearnContentType view) {
        if (view != null) {
            learnContentTypeService.update(view);
        }
        return "redirect:/learn/contenttype/list";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestParam("id") String id) {
        Result data = Result.failure("操作失败");
        if (id != null) {
            try {
                learnContentTypeService.delete(id);
                data = Result.success();
            } catch (Exception e) {
                data = Result.failure(e.getMessage());
            }
        }
        return data;
    }
}
